package com.yzf.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzf.common.R;
import com.yzf.entity.Category;
import com.yzf.entity.Dish;
import com.yzf.entity.Employee;
import com.yzf.mapper.CategoryMapper;
import com.yzf.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yzf
 * @description 分类管理
 * @since 2023/2/15 22:16
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        // 分页构造器
        Page<Category> pg = new Page(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        // 添加拍序条件
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pg,queryWrapper);
        return R.success(pg);
    }

    @PostMapping
    public R<String> save(@RequestBody Category category){
//        category.setIsDeleted(0);
        if(categoryService.save(category)){
            return R.success("分类添加成功");
        }
        return R.error("添加操作失败");
    }

    @DeleteMapping
    public R<String> delete(Long ids){
        categoryService.remove(ids);
        return R.success("分类信息删除成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){
        if(categoryService.updateById(category)){
            return R.success("修改分类成功");
        }
        return R.success("修改分类成功");
    }

    @GetMapping("/list")
    public R<List<Category>> getList(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort);
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }

}
